package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeysUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

//@Component
public class RedisDateInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        // 获取数据库中攻略相关数据
/*        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail strategyDetail : list) {
            // 判断 数据是否已经存在 redis中
            // 如果存在就跳过
            // 如果不存在就添加
            if (strategyStatisVOService.isStrategyExists(strategyDetail.getId())) {
                continue;
            }
            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(strategyDetail, vo);
            vo.setDestId(strategyDetail.getDest().getId());
            vo.setDestName(strategyDetail.getDest().getName());
            vo.setStrategyId(strategyDetail.getId());
            // 存入redis中
            strategyStatisVOService.saveInitData(vo);
        }*/

        // 热门排行榜 数据初始化
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail strategyDetail : list) {
            if (strategyStatisVOService.isZsetExstis(RedisKeysUtil.STRATEGY_STATIS_HOT.getRedisKeyPre(), strategyDetail.getId())) {
                continue;
            }

            strategyStatisVOService.addHotScore(strategyDetail.getId(), strategyDetail.getViewnum() + strategyDetail.getReplynum());
        }

        // 国内外排行榜初始化数据
        for (StrategyDetail strategyDetail : list) {
            if (strategyStatisVOService.isZsetExstis(RedisKeysUtil.STRATEGY_STATIS_RANK.getRedisKeyPre(), strategyDetail.getId())) {
                continue;
            }
            strategyStatisVOService.addAbroadOrUnAbroadStrategyScore(strategyDetail.getId(), strategyDetail.getFavornum() + strategyDetail.getThumbsupnum());
        }

    }
}
